package control.report;

import java.io.File; 
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date; 
import java.util.List;

import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintException;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.SimpleDoc;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.Copies;
import javax.print.attribute.standard.MediaSizeName;

import jxl.*; 
import jxl.read.biff.BiffException;
import jxl.write.*;
import jxl.write.Number;
import jxl.write.biff.RowsExceededException;


public class ExcelHandler {
	private WritableWorkbook copy;
	private String path;
	private String secondpath;
	private String newFilePath;
	private WritableSheet sheet;
	private boolean print;

	
	
	
	public boolean isPrint() {
		return print;
	}

	public void setPrint(boolean print) {
		this.print = print;
	}

	public String getNewFilePath() {
		return newFilePath;
	}

	public void setNewFilePath(String newFilePath) {
		this.newFilePath = newFilePath;
	}

	public void importFile(){ 
		
		String folder;
		if (print){
			folder="print";
		}
		else{
			folder="download";
		}
		File file = new File("C:/Documents and Settings/eneida/workspace/Protubo/Reports/orcamento/"+path+".xls");
		try {
			Workbook workbook = Workbook.getWorkbook(file);
			Integer i=0;

			newFilePath="C:/Documents and Settings/Yukon/workspace/Protubo/Reports/orcamento/"+folder+"/"+path+"temp"+i.toString()+".xls";
			
			boolean exist=true;
			while (exist){
				File file2 = new File(newFilePath);
				exist=file2.exists();
				if (exist) {
					i++;
					newFilePath="C:/Documents and Settings/Yukon/workspace/Protubo/Reports/orcamento/temp/"+path+"temp"+i.toString()+".xls";
				}
			}
			
			this.copy = Workbook.createWorkbook(new File(newFilePath), workbook);
			this.sheet = copy.getSheet(0); 
			
			// All cells modified/added. Now write out the workbook 


		} catch (BiffException e) {
			// TODO Auto-generated catch block
			try {
				copy.close();
			} catch (WriteException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			try {
				copy.close();
			} catch (WriteException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			e.printStackTrace();
		} 
	}
	
	public void closeFile(){
			try {
				try {
					copy.close();
				} catch (WriteException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}
	public void writeFile(){
		try {
				copy.write();

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
}
	public void addString(int row, int column, String text){
		Label label = new Label(column-1, row-1, text); 
		try {
			sheet.addCell(label);
		} catch (RowsExceededException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (WriteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
	}
	public void addNumber(int row, int column, Double number){
		Number num = new Number(column-1,row-1,number); 
		try {
			sheet.addCell(num);
		} catch (RowsExceededException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (WriteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
	}
	  public void Print_doc() {
	       DocFlavor format = DocFlavor.INPUT_STREAM.TEXT_PLAIN_US_ASCII;
	    //DocFlavor format = new DocFlavor("text/plain", "java.io.FileInputStream");
	       PrintRequestAttributeSet aset =new HashPrintRequestAttributeSet();
	 
	       aset.add(MediaSizeName.ISO_A4);
	       aset.add(new Copies(1));
	       //aset.add(Sides.ONE_SIDED);
	       //aset.add(Finishings.NONE);
	 
	       PrintService pservices=PrintServiceLookup.lookupDefaultPrintService();
	 
	       System.out.println(pservices.getName());
	       DocPrintJob djob=pservices.createPrintJob();
	 
	       try{
	       FileInputStream input =new FileInputStream("report.xls");
	       Doc doc = new SimpleDoc(input,format,null);
	       djob.print(doc,aset);
	 
	       }
	       catch(FileNotFoundException exc){
	         exc.printStackTrace();
	         System.out.println("File not found");
	       }
	       catch(PrintException e){
	         e.printStackTrace();
	         System.out.println("Printer not found");
	         }
	  }

	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}
	private List paths=new ArrayList();
	public List getPaths() {
		return paths;
	}

	public void setPaths(List paths) {
		this.paths = paths;
	}


	public String getSecondpath() {
		return secondpath;
	}

	public void setSecondpath(String secondpath) {
		this.secondpath = secondpath;
	}
}
